package com.zenith.zongheng.demo.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenith.zongheng.core.exception.ProcessException;
import com.zenith.zongheng.demo.constant.LeaveResultConstant;
import com.zenith.zongheng.demo.domain.entity.LeaveFile;
import com.zenith.zongheng.demo.dao.LeaveFileMapper;
import com.zenith.zongheng.demo.api.LeaveFileService;
import com.zenith.zongheng.demo.domain.dto.LeaveFileDTO;
import com.zenith.zongheng.demo.domain.dto.LeaveFileSaveDTO;
import com.zenith.zongheng.demo.domain.entity.LeaveInfo;
import com.zenith.zongheng.demo.domain.vo.LeaveFileVO;
import com.zenith.zongheng.demo.converter.LeaveFileConverter;
import com.zenith.zongheng.common.domain.vo.PageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.zongheng.demo.tool.FileOp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tangzx
 * @since 2021-12-07
 */

@Service
@org.apache.dubbo.config.annotation.Service
public class LeaveFileServiceImpl extends ServiceImpl<LeaveFileMapper, LeaveFile> implements LeaveFileService {

    @Autowired
    private LeaveFileConverter leaveFileConverter;

    @Override
    public PageVO<LeaveFileVO> queryPage(LeaveFileDTO dto) {
        IPage<LeaveFileVO> page = baseMapper.selectPage(new Page<>(dto.getCurrPage(), dto.getPageSize()), dto);
        return new PageVO<>(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void saveLeaveFile(String filePath, String fileName,Integer leaveInfoId) {
        LeaveFileSaveDTO dto = new LeaveFileSaveDTO();
        dto.setFilePath(filePath);
        dto.setFilename(fileName);
        dto.setLeaveInfoId(leaveInfoId);
        LeaveFile entity = leaveFileConverter.dto2LeaveFile(dto);
        baseMapper.insert(entity);
    }


    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void updateLeaveFile(LeaveFileSaveDTO dto) {
        LeaveFile entity = leaveFileConverter.dto2LeaveFile(dto);
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void deleteLeaveFile(Long... ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void uploadFile(Integer leaveInfoId, MultipartFile file) {
        if(file.isEmpty()){
            throw new ProcessException(LeaveResultConstant.FILE_IS_EMPTY_ERR);
        }
        //连表查询
        LeaveInfo leaveInfo = baseMapper.getLeaveInfoById(leaveInfoId);
        try {
            //本地储存
            String[] fileDst = FileOp.uploadFile(
                    leaveInfo.getUserName(), leaveInfo.getCreateTime().toString(), file
            );
            //数据库储存
            saveLeaveFile(fileDst[0], fileDst[1],leaveInfoId);
        } catch (FileNotFoundException e){
                throw new ProcessException(LeaveResultConstant.FILE_NOT_FOUND_ERR);
        } catch (IOException e) {
            throw new ProcessException(LeaveResultConstant.FILE_UPLOAD_ERR);
        }


    }
}
